package com.ch.movie

import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2.ORIENTATION_VERTICAL
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.ch.common.base.fragment.BaseFragment
import com.ch.movie.adapter.MovieAdapter
import com.ch.movie.adapter.NormalViewHolder
import com.ch.movie.databinding.MovieFragmentBinding
import com.ch.movie.viewModel.MovieViewModel
import com.shuyu.gsyvideoplayer.GSYVideoManager

class MovieFragment : BaseFragment<MovieViewModel, MovieFragmentBinding>() {

    override val viewModel: MovieViewModel by viewModels()
    private lateinit var movieAdapter: MovieAdapter
    override fun bindViewModel() {
        mBinding.viewModel = viewModel
    }

    override fun getLayoutResID(): Int {
        return R.layout.movie_fragment
    }

    override fun initView() {
        mBinding.movieViewPager2.orientation = ORIENTATION_VERTICAL
        mBinding.movieViewPager2.offscreenPageLimit=1
        movieAdapter = MovieAdapter(viewModel.itemDataList)
        mBinding.movieViewPager2.adapter = movieAdapter

        mBinding.movieViewPager2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                //大于0说明有播放
                val playPosition = GSYVideoManager.instance().playPosition
                if (playPosition >= 0) {
                    //对应的播放列表TAG
                    if (GSYVideoManager.instance().playTag == NormalViewHolder.TAG && position != playPosition) {
                        playPosition(position)
                    }
                }
            }
        })
        mBinding.movieViewPager2.post { playPosition(0) }
    }

    override fun initObserve() {
        viewModel?.run {
            getMovieData().observe(this@MovieFragment) {
                if (it.isSuccess) {
                    val bean = it.getOrNull()
                    bean?.run {
                        viewModel.itemDataList.addAll(bean?.items)
                        movieAdapter.notifyDataSetChanged()
                        mBinding.movieViewPager2.post { playPosition(0) }
                    }
                }

            }
        }
    }

    private fun playPosition(position: Int) {
        val viewHolder =
            (mBinding.movieViewPager2.getChildAt(0) as RecyclerView)
                .findViewHolderForAdapterPosition(position)

        if (viewHolder != null) {
            val recyclerItemNormalHolder: NormalViewHolder =
                viewHolder as NormalViewHolder
            recyclerItemNormalHolder.getPlayer()?.startPlayLogic()
        }
    }

    override fun onResume() {
        super.onResume()
        GSYVideoManager.onResume()
    }

    override fun onPause() {
        super.onPause()
        GSYVideoManager.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
    }
}